package com.micro.data;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;

public class AccountInfo {
	private static final String ACCOUNT_FILE = "account.xml";
	private String uid;
	private String screenName;
	private String userName;
	private String location;
	private String description;
	private String profileImageUri;
	private String domain;
	private String gender;
	private String followersCount;
	private String friendsCount;
	private String statusCount;
	private String favouritesCount;
	private String createDate;
	private String following;
	private String verified;
	private String token;
	private String tokenScret;

	private static Context context;

	public static void setContext(Context context) {
		AccountInfo.context = context;
	}

	public String getUid() {
		return uid;
	}

	public String getScreenName() {
		return screenName;
	}

	public String getUserName() {
		return userName;
	}

	public String getProfileImageUri() {
		return profileImageUri;
	}

	public String getToken() {
		return token;
	}

	public String getTokenScret() {
		return tokenScret;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public void setScreenName(String screenName) {
		this.screenName = screenName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setTokenScret(String tokenScret) {
		this.tokenScret = tokenScret;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setProfileImageUri(String profileImageUri) {
		this.profileImageUri = profileImageUri;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public void setFollowersCount(String followersCount) {
		this.followersCount = followersCount;
	}

	public void setFriendsCount(String friendsCount) {
		this.friendsCount = friendsCount;
	}

	public void setStatusCount(String statusCount) {
		this.statusCount = statusCount;
	}

	public void setFavouritesCount(String favouritesCount) {
		this.favouritesCount = favouritesCount;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public void setFollowing(String following) {
		this.following = following;
	}

	public void setVerified(String verified) {
		this.verified = verified;
	}

	public static AccountInfo loadAccount() {
		FileInputStream fis = null;
		AccountInfo account = null;
		try {
			fis = context.openFileInput(ACCOUNT_FILE);
			account = getAccountFrom(fis);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return account;
	};

	private static AccountInfo getAccountFrom(InputStream is)
			throws XmlPullParserException, IOException {
		AccountInfo accountData = null;
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		factory.setNamespaceAware(true);
		XmlPullParser xmlPullParser = factory.newPullParser();
		xmlPullParser.setInput(is, "UTF-8");
		if (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {
			if (xmlPullParser.getEventType() == XmlPullParser.START_TAG) {
				if (xmlPullParser.getName().equals("account")) {
					accountData = new AccountInfo();
					while (xmlPullParser.next() != XmlPullParser.END_DOCUMENT) {
						if (xmlPullParser.getEventType() == XmlPullParser.START_TAG) {
							if (xmlPullParser.getName().equals("id")) {
								xmlPullParser.next();
								accountData.setUid(xmlPullParser.getText());
								continue;
							}
							if (xmlPullParser.getName().equals("screen_name")) {
								xmlPullParser.next();
								accountData.setScreenName(xmlPullParser
										.getText());
								continue;
							}
							if (xmlPullParser.getName().equals("user_name")) {
								xmlPullParser.next();
								accountData
										.setUserName(xmlPullParser.getText());
								continue;
							}
							if (xmlPullParser.getName().equals("description")) {
								xmlPullParser.next();
								accountData.setDescription(xmlPullParser
										.getText());
								continue;
							}
							if (xmlPullParser.getName().equals(
									"profile_image_uri")) {
								xmlPullParser.next();
								accountData.setProfileImageUri(xmlPullParser
										.getText());
								continue;
							}
							if (xmlPullParser.getName().equals("token")) {
								xmlPullParser.next();
								accountData.setToken(xmlPullParser.getText());
								continue;
							}
							if (xmlPullParser.getName().equals("secret_token")) {
								xmlPullParser.next();
								accountData.setTokenScret(xmlPullParser
										.getText());
								continue;
							}
						}

						if (xmlPullParser.getEventType() == XmlPullParser.END_TAG) {
							if (xmlPullParser.getName().equals("account")) {
								break;
							}
						}
					}
				}
			}
		}

		return accountData;
	}

	public void persist() {
		try {
			FileOutputStream fos = context.openFileOutput(ACCOUNT_FILE,
					Context.MODE_PRIVATE);
			PrintWriter pw = new PrintWriter(fos);
			StringBuffer sb = new StringBuffer(
					"<?xml version='1.0' encoding='utf-8' ?> <account>");
			sb.append(getAccountInfoXML());
			sb.append("<Account>");
			pw.write(sb.toString());
			pw.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String getAccountInfoXML() {
		StringBuffer accountXML = new StringBuffer();
		accountXML.append("<id>").append(uid).append("</id>")
				.append("<screen_name>").append(screenName)
				.append("</screen_name>").append("<user_name>")
				.append(userName).append("</user_name>")
				.append("<description>").append(description)
				.append("</description>").append("<profile_image_uri>")
				.append(profileImageUri).append("</profile_image_uri>")
				.append("<token>").append(token).append("</token>")
				.append("<secret_token>").append(tokenScret)
				.append("</secret_token>");
		return accountXML.toString();
	}

}
